﻿using CPPEI.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace TestCaptureScreen
{
    /// <summary>
    /// 屏幕数据渲染视图
    /// </summary>
    public class ScreenDataRenderView : UIElement
    {
        private int _x;
        private int _y;
        private int _width;
        private int _height;

        WriteableBitmap _wb;

        public ScreenDataRenderView()
        {
            _wb = new WriteableBitmap(800, 400, 96, 96, System.Windows.Media.PixelFormats.Bgr555, null);
            //_wb = new WriteableBitmap(200, 100, 96, 96, System.Windows.Media.PixelFormats.Bgr555, null);
        }

        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            drawingContext.DrawImage(_wb, new Rect(0, 0, 800, 400));
            //drawingContext.DrawImage(_wb, new Rect(0, 0, 200, 100));

            base.OnRender(drawingContext);
        }

        /// <summary>
        /// 更新屏幕数据
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="screenWidth"></param>
        /// <param name="screenHeight"></param>
        /// <param name="data"></param>
        public void Update(int x, int y, int width, int height, int screenWidth, int screenHeight, byte[] data)
        {
            this._x = x;
            this._y = y;
            this._width = width;
            this._height = height;

            //byte[] buffer = new byte[width * height * 3];
            //for (int r = 0; r < height; r++)
            //{
            //    for (int c = 0; c < width; c++)
            //    {
            //        int index = r * c * 3;
            //        buffer[index] = 0xFF;
            //        buffer[index + 1] = 0xFF;
            //        buffer[index + 2] = 0;
            //    }
            //}

            byte[] originalBuffer = GZip.Decompress(data);
            //_wb.WritePixels(new Int32Rect(x, y, this._width, this._height), GZip.Decompress(data), 200, 0);
            _wb.WritePixels(new Int32Rect(x, y, this._width, this._height), originalBuffer, 800, 0);
            InvalidateVisual();
        }
    }
}
